# -*- coding:utf-8 -*-
from __future__ import absolute_import, unicode_literals

"""
本文件记录数据库相关配置
"""
# 网站个性化配置
WEBSITE_CONF = {
    "website_name": "国网电商征信平台",
    "website_corp": "国网电子商务有限公司",
    "website_proxy": True
}

# 重新分配
SECRET_KEY = "2b55264e-ccab-4e28-8f1d-903760347665328f57b5-b9fa-4067-b17a-fa1d2b61296d49b338c8-d0f9-436a-b495-5ed76015f049"
NEVERCACHE_KEY = "8067f916-05f2-4d95-b63e-9ddfdcae86e126e87476-dbff-41b9-8210-525a81368b1349adabf1-3b9a-4267-b033-e3fb78528d95"

# 搜索引擎地址，精确到 /index/type/
elasticsearch_host = "http://192.168.90.12:9200/"

# 图数据库地址，如：bolt://172.16.204.30/
neo4jhost_host = "bolt://192.168.90.12:7474/"

# 缓存设置
# CACHES = {
#    "default": {
#        "BACKEND": "django_redis.cache.RedisCache",
#        "LOCATION": "redis://:longfish@127.0.0.1:6379/0",
#        "OPTIONS": {
#            "CLIENT_CLASS": "django_redis.client.DefaultClient",
#        }
#    }
# }

# 数据库配置

tidb_host = "192.168.90.14"
mysql_test_host = "192.168.80.169"
target_host = mysql_test_host

DATABASES = {
    "default": {
        "ENGINE": "mainsys.mysqlpool",
        "NAME": "el_guowang",
        "USER": "credit",
        "PASSWORD": "Credit_12345",
        "HOST": target_host,
        "PORT": "13306"
    },
    "company": {
        "ENGINE": "mainsys.mysqlpool",
        "NAME": "el_company",
        "USER": "credit",
        "PASSWORD": "Credit_12345",
        # "HOST": "192.168.90.14",
        "HOST": target_host,
        "PORT": "13306",
    },
    "companyplus": {
        "ENGINE": "mainsys.mysqlpool",
        "NAME": "el_companyplus",
        "USER": "credit",
        "PASSWORD": "Credit_12345",
        #   "HOST": "192.168.90.14",
        "HOST": target_host,
        "PORT": "13306"
    },
    "dataplus": {
        "ENGINE": "mainsys.mysqlpool",
        "NAME": "el_dataplus",
        "USER": "credit",
        "PASSWORD": "Credit_12345",
        # "HOST": "192.168.90.14",
        "HOST": target_host,
        "PORT": "13306"
    }
}


# 配置多数据库路由规则，实现读写分离等操作
class MasterSlaveRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label in ['company', ]:
            return 'company'
        elif model._meta.app_label in ['companyplus', ]:
            return 'companyplus'
        elif model._meta.app_label in ['codes', 'companylaw', 'dataplus', ]:
            return 'dataplus'
        return 'default'

    def db_for_write(self, model, **hints):
        if model._meta.app_label in ['company', ]:
            return 'company'
        elif model._meta.app_label in ['companyplus', ]:
            return 'companyplus'
        elif model._meta.app_label in ['codes', 'companylaw', 'dataplus', ]:
            return 'dataplus'
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_syncdb(self, db, model):
        if model._meta.app_label in ["company", ] and db in ["company", ]:
            return "company"
        elif model._meta.app_label in ['companyplus', ] and db in ['companyplus', ]:
            return 'companyplus'
        elif model._meta.app_label in ['codes', 'companylaw', 'dataplus', ] and db in ['codes', 'companylaw',
                                                                                       'dataplus', ]:
            return 'dataplus'
        else:
            return 'default'


# 如需启用配置的多数据库路由规则，则反注释掉下面语句
DATABASE_ROUTERS = ['mainsys.dbconfig.MasterSlaveRouter']
